package com.glodon.paas.databag.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.glodon.paas.databag.service.DatabagService;
import com.glodon.paas.databag.sso.SecurityUtils;

@Controller
@RequestMapping("/")
public class HomeController {
    public static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class);
    @Autowired
    private DatabagService databagService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView root(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        return index(request, response);
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        if (!SecurityUtils.isAuthenticated()) {
            request.getRequestDispatcher("api/sso/signin").forward(request, response);
            return null;
        }
        return new ModelAndView("dwgrvt");
    }
    
    @RequestMapping(value = "/page/{type}", method = RequestMethod.GET)
    public ModelAndView page(@PathVariable("type")String type, HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        String viewType = databagService.getDatabagType(type);
        if ("rvt".equals(viewType) || "dwg".equals(viewType)) {
            viewType = "dwgrvt";
        } else if ("doc".equals(viewType)) {
            viewType = "office";
        }
        return new ModelAndView(viewType);
    }
}
